<?php
/**
 * @Author: kaihui.wang
 * @Contact  hpuwang@gmail.com
 * @Version: 1.0.0
 * @Date: 2020/11/2 10:14
 */

namespace Eduxplus\WebsiteBundle\Controller\Web;


use Eduxplus\CoreBundle\Lib\Base\BaseHtmlController;
use Eduxplus\CoreBundle\Lib\Service\HelperService;
use Eduxplus\WebsiteBundle\Service\GlobService;
use Eduxplus\WebsiteBundle\Service\NewsService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class NewsController extends BaseHtmlController
{

    
    public function indexAction($category=0,$topValue=0,$page=1, NewsService $newsService, GlobService $globService){
        $page = $page?$page:1;
        $pageSize = 20;
        list($pagination, $list) = $newsService->getNewsList($category, $topValue, $page, $pageSize);
        $newsCategory = $newsService->getAllNewsFirstCategory();
        $notices = $newsService->getNewsByCategory(1, 3);//公告
        $categoryInfo = $newsService->getCategoryById($category);
        $data = [];
        $data["newsCategory"] = $newsCategory;
        $data["notices"] = $notices;
        $data['list'] = $list;
        $data['oneSentenceADay']= $globService->getOneSentenceADay();
        $data['pagination'] = $pagination;
        $data['category'] = $category;
        $data['topValue'] = $topValue;
        $data['route'] = "app_news";
        $data['seoTitle']=$categoryInfo && isset($categoryInfo['name'])?$categoryInfo['name']."-文章":"文章";
        $data['seoDescr']="文章列表";
        $seoKw = "";
        if($newsCategory){
            foreach($newsCategory as $cate){
                $seoKw .= $cate['name'].",";
            }
        }
        $seoKw = trim($seoKw, ",");
        $data['seoKw']=$seoKw;
        return $this->render("@WebsiteBundle/news/index.html.twig", $data);
    }

    
    public function detailAction($id, NewsService $newsService, HelperService $helperService){
        $newsService->viewNumIncre($id);
        $detail = $newsService->getById($id);
        if(!$detail || !$detail["status"]){
            return $this->render("@WebsiteBundle/404.html.twig");
        }
        $data = [];
        $data['detail'] = $detail;
        $data['route'] = "app_news";
        $data['seoTitle']= $detail['title'];
        $data['seoDescr']= substr($detail['main']['content'], 0,100);
        $data['seoKw']=$detail['tags'];
        $uid = $this->getUid();
        $data['fav'] = [];
        if($uid){
            $data['fav'] = $newsService->getFav($id, $uid);
        }
        return $this->render("@WebsiteBundle/news/detail.html.twig", $data);
    }

    public function doFavAction($id, NewsService $newsService){
        $uid = $this->getUid();
        $check = $newsService->getFav($id, $uid);
        if($check){
            $newsService->delFav($id, $uid);
            $newsService->favNumDecre($id);
            return $this->responseSuccess(["fav"=>0], "取消收藏成功!");
        }else{
            $newsService->addFav($id, $uid);
            $newsService->favNumIncre($id);
            return $this->responseSuccess(["fav"=>1],"收藏成功!");
        }
    }

    public function doClearFailureAction(NewsService $newsService){
        $uid = $this->getUid();
        $newsService->clearFailure($uid);
        return $this->responseMsgReload("操作成功!");
    }

}
